<template>
    <div class="login">
        <div class="header">后台管理系统</div>
        <div class="content">
            <div class="card">
                <div class="title">登录后台管理系统</div>
                <a-input class="input" v-model:value="params.phone" placeholder="请输入您的账号或手机号"></a-input>
                <a-input v-if="params.type === 1" v-model:value="params.password" class="input" type="password"
                         placeholder="请输入密码"></a-input>
                <div v-else class="code">
                    <a-input placeholder="请输入验证码" v-model:value="params.code"></a-input>
                    <a-button class="codeBtn" type="primary" @click="getCode">
                        {{ params.isCounting ? `${params.countdown}s` : '获取验证码' }}
                    </a-button>
                </div>
                <a-button class="button" type="primary" @click="loginTodo">登录</a-button>
                <div class="operation">
                    <div style="margin-right: 12px" @click="changeLogin">
                        {{ params.type === 1 ? '短信验证码登录' : '密码登录' }}
                    </div>
                    <div>忘记密码</div>
                </div>
            </div>
        </div>
        <div class="footer">
            <div class="link item">
                <div>关于我们</div>
                <div>联系我们</div>
                <div>法律声明</div>
                <div>隐私政策</div>
            </div>
            <div class="copy item">© 2016-现在 浙江禾刀科技有限公司 版权所有 增值电信业务经营许可证：浙B2-20160869</div>
            <div class="ban item">浙公网安备 33010602010281号</div>
        </div>
    </div>
</template>
<script setup>
import {reactive} from 'vue'
import {message} from 'ant-design-vue'
import { useRouter } from 'vue-router';
const router = useRouter();
import {getMessage, login} from '../../api/login.js'
import adminRoutes from '../../router/admin.js'
const params = reactive({
    type: 1,
    phone: '',
    password: '',
    code: '',
    isCounting: false,
    countdown: 60
})
const changeLogin = () => {
    params.type = params.type === 1 ? 2 : 1;
    params.password = ''
    params.code = ''
}
const loginTodo = () => {
    let data = {
        phone: params.phone,
        password: params.password,
        // logType: params.type,
        // projectId: 1,
        // code: params.code,
    }
    login(data).then(res => {
        if (res?.code === 200) {
            localStorage.setItem('userInfo', JSON.stringify(res.data))
            localStorage.setItem('token', res.data.token)
            // getInfo().then(res => {
            //     localStorage.setItem('user', JSON.stringify(res.data))
            // })
            let configRouter = JSON.parse(JSON.stringify(adminRoutes)) || []
            router.push('/project/index')
            // menusList().then(rs => {
            //     for(let i=configRouter.length-1;i--;i>=0){
            //         let demo = configRouter[i];
            //         let flag = rs.data.findIndex(item=>{return item.aid === demo.id})
            //         if(flag >=0){
            //             demo.path = rs.data[flag].id
            //             demo.name=  rs.data[flag].id
            //             demo.meta.title =  rs.data[flag]?.title
            //             if(demo.children){
            //                 for(let k = demo.children.length-1;k--;i>=0){
            //                     let p = demo.children[k]
            //                     let findObj = getChildren(rs.data, p.id)
            //                     if(findObj){
            //                         p.path = findObj.id
            //                         p.name=  findObj.id
            //                         p.meta.title =  findObj.title
            //                     }else{
            //                         demo.children.splice(k,1)
            //                     }
            //                 }
            //             }
            //         }else{
            //             configRouter.splice(i,1)
            //         }
            //     }
            //     localStorage.setItem('menus', JSON.stringify(configRouter))
            //     router.push('/home')
            // })
        }else{
            message.warn(res?.message)
        }
    })
}
const getChildren = (arr,id) => {
    let obj = null
    arr.map(item=>{
        if(item.children && item.children.length){
            item.children.map(demo=>{
                if(demo.aid === id) obj =  demo
            })
        }
    })
    return obj
}
const getCode = () => {
    if (params.isCounting) return;
    if (!params.phone) return message.warning('请输入您的账号或手机号');
    getMessage({phone: params.phone, projectId: 1}).then(res => {
        if (res?.code === 200) {
            params.isCounting = true;
            const timer = setInterval(() => {
                if (params.countdown > 0) {
                    params.countdown--;
                } else {
                    clearInterval(timer);
                    params.isCounting = false;
                    params.countdown = 60;
                }
            }, 1000);
        } else {
            message.warn(res?.message)
        }
    })
}
</script>
<style scoped>
@import "./style.less";
</style>
